package com.clientservice;

import com.qqcommon.Message;
import com.qqcommon.MessageType;

import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Date;

/**
 * @author wty
 * @date 2022/10/26 17:30
 * 该类 提供 和客户端私聊、群聊的相关方法
 */
@SuppressWarnings({"all"})
public class MessageClientService {
    // 私聊
    public void sendMessagetoOne(String content, String receiver, String sender) throws Exception { // 消息内容，消息接收方，消息发送方
        Message message = new Message();
        message.setSender(sender);
        message.setContent(content);
        message.setReceiver(receiver);
        message.setSendTime(new Date().toString()); // 日期类：记录发送时间
        message.setType(MessageType.MESSAGE_COMMON);
        System.out.println(sender+"对"+receiver+"说：" + content);

        // 从线程中取集合socket即可
        Socket socket = ManageClientConnectServerThread.get(sender).getSocket();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
        objectOutputStream.writeObject(message);
    }
    // 群聊
    public void sendMessagetoEveryOne(String content, String sender) throws Exception { // 消息内容，消息接收方，消息发送方
        Message message = new Message();
        message.setSender(sender);
        message.setContent(content);
        // 日期类：记录发送时间
        message.setSendTime(new Date().toString());
        message.setType(MessageType.MESSAGE_EVERYONE);
        System.out.println(sender+"对大家说：" + content);

        // 从线程中取集合socket即可
        Socket socket = ManageClientConnectServerThread.get(sender).getSocket();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
        objectOutputStream.writeObject(message);
    }

}
